package com.enjoy.springboot.server;

import com.enjoy.springboot.interfaces.WebServer;
import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class TomcatServer implements WebServer {
    @Override
    public void start(WebApplicationContext context) {
        System.out.println("启动Tomcat");
        startTomcat(context);
    }

    /**
     * 启动Tomcat
     */
    public void startTomcat(WebApplicationContext webApplicationContext) {
        Tomcat tomcat = new Tomcat();
        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(8081);

        Engine engine = new StandardEngine();

        engine.setDefaultHost("localhost");

        Host host = new StandardHost();

        host.setName("localhost");

        String contextPath = "";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);

        service.addConnector(connector);

        tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(webApplicationContext));

        context.addServletMappingDecoded("/*", "dispatcher");
        try {
            tomcat.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("tomcat启动完成");
    }

}
